// 天气 API 工具函数
export const getWeatherData = async (latitude: number | string, longitude: number | string) => {
  const key = "071890e933db4db5b7483a958d1c3e44";
  const url = `https://devapi.qweather.com/v7/weather/now?key=${key}&location=${longitude},${latitude}`;
  try {
    const response = await fetch(url);
    if (!response.ok) {
      throw new Error(`天气API请求失败: ${response.status}`);
    }
    const result = await response.json();
    
    if (result.code !== '200') {
      throw new Error(`天气API返回错误: ${result.code} - ${result.msg || '未知错误'}`);
    }
    
    const weatherData = result.now;
    if (!weatherData) {
      throw new Error('天气数据格式错误');
    }
    
    return {
      humidity: weatherData.humidity + '%',
      text: weatherData.text,
      temp: weatherData.temp + '°C',
      windDir: weatherData.windDir,
      windScale: weatherData.windScale,
      vis: weatherData.vis,
      updateTime: result.updateTime
    };
  } catch (err) {
    console.error("天气数据加载失败", err);
    throw new Error(`无法获取天气数据: ${err instanceof Error ? err.message : '未知错误'}`);
  }
};
